<?php
$isReloadable = false;
require_once "../../../pers/inc.config.php";
require_once OVF_FS_WEB_ROOT.'/app/_inc/inc.session.php';
require_once OVF_FS_WEB_ROOT.'/app/_inc/funct.html.php';
require_once '../_inc/class.wfmModulo.php';
//require_once '../_inc/class.wfmPagina.php';

$oMod = new wfmModulo(reqVarInt('id'));
if ($oMod->codModulo == "") {
	backOnErr("id.'".reqVar('id')."' errato");
	exit;
}
$idModulo = $oMod->idModulo;
$codModulo = $oMod->codModulo;

$sSql = "SELECT r.* FROM wfm_rec_$codModulo r WHERE r.id_rec = ".reqVarInt('idr');
$objDB->query($sSql,'selRecX');
$aRec = $objDB->fetch('selRecX');
if (!$aRec) {
	backOnErr("id_rec '".reqVar('idr')."' errato");
	exit;
}

if (reqVar('azione') == 'registra') {
	$msgPag = azioneRegistra();
} 

if (reqVar('azione') == 'elimina') {
	$msgPag = azioneElimina();
} 


$sCmdR = "";
$sCmdR .= "<a href=\"javascript:confElim();\" class=\"cmd1\">elimina</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a href=\"javascript:submAz('registra');\" class=\"cmd1\">registra</a>";
$sCmdR .= cmdTorna();

testaHtml($oMod->titolo,'ZAP - fare attenzione !',$msgPag,'wfma');
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
function confElim() {
	if (confirm("Confermi l'eliminazione ?")) {
		document.frm1.azione.value = 'elimina';
		document.frm1.submit();
	}
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo  $sCmdR;?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm('frm1','',array('enctype'=>true));
echo getHtmlHidden('azione','...');
echo getHtmlHidden('id');
echo getHtmlHidden('idr');
echo getHtmlHidden('xsn',XSN);
foreach($aRec as $sNomX => $valX) {
	$sANomX = "A_".$sNomX;
	if (isset($oMod->aCampi[$sNomX])) {
		$aCX = $oMod->aCampi[$sNomX];
		switch($aCX['tip']) {
			case 'numero':
				reqDef($sANomX, num2str($aRec[$sNomX]),$aCX['dec']);
				break;
			case 'intero':
				reqDef($sANomX, intval($aRec[$sNomX]));
				break;
			case 'data':
				reqDef($sANomX, YMD2DMY($aRec[$sNomX]));
				break;
			case 'testo':
				reqDef($sANomX, $aRec[$sNomX]);
				break;
			default:
				reqDef($sANomX, $aRec[$sNomX]);
				break;
		}
	} else {
		reqDef($sANomX, $aRec[$sNomX]);
	}
}
echo "<table cellpadding='3' cellspacing='1' width='100%'>\n";
echo $oMod->getHtmlCampiStd('zap');
echo "</table>\n";
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         <? echo  $sCmdR;?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function azioneElimina() {
	global $objSess, $objDB, $oMod;
	$sSql = "DELETE FROM wfm_rec_".$oMod->codModulo; 
	$sSql .= " WHERE id_rec = ".reqVar('idr');
	$objDB->exec($sSql);
	$sSql = "DELETE FROM wfm_rec_".$oMod->codModulo."_log "; 
	$sSql .= " WHERE id_rec = ".reqVar('idr');
	$objDB->exec($sSql);
	header("location: recList.php?id=".$oMod->idModulo."&xsn=".XSN);
	exit;
}
	
function azioneRegistra() {
	global $objSess, $objDB, $oMod;
	$msg = "";
	$aDatiInp = array();
	foreach($_REQUEST as $nX => $vX) {
		$aDatiInp[$nX] = reqVar($nX);
	}
	foreach($oMod->aCampi as $sNomX => $aCX) {
		switch($aCX['tip']) {
			case 'data':
				if (trim($aDatiInp["A_".$sNomX]) != ""
				&& str2date($aDatiInp["A_".$sNomX]) === false) {
					$msg .= "campo '".$aCX['lab']."' - data errata<br>";
				}
				break;
			case 'numero':
			case 'intero':
				if (trim($aDatiInp["A_".$sNomX]) != ""
				&& str2num($aDatiInp["A_".$sNomX]) === false) {
					$msg .= "campo '".$aCX['lab']."' - numero errato<br>";
				}
				break;
			default:
				break;
		}
	}		
	if ($msg != "") {
		return $msg;
	}
	$aDatiDB = array();
	foreach($oMod->aCampi as $sNomX => $aCX) {
		switch($aCX['tip']) {
			case 'data':
				$aDatiDB["A_".$sNomX] = DMY2YMD($aDatiInp["A_".$sNomX]);
				break;
			case 'numero':
			case 'intero':
				$aDatiDB["A_".$sNomX] = str2num($aDatiInp["A_".$sNomX]);
				break;
			case 'si/no':
				$aDatiDB["A_".$sNomX] = ($aDatiInp["A_".$sNomX]=='S')?'S':'N';
				break;
			default:
				$aDatiDB["A_".$sNomX] = $aDatiInp["A_".$sNomX];
				break;
		}
	}
	$sSql = "UPDATE wfm_rec_".$oMod->codModulo; 
	$sSql .= " SET stato = ".$aDatiInp['A_stato'];
	$sSql .= ", id_pagina = ".$aDatiInp['A_id_pagina'];
	foreach($oMod->aFields as $sField => $aFX) {
		if ($sField == 'id_rec' || $sField == 'id_modulo' 
		|| !isset($aDatiDB['A_'.$sField])) {
			continue;
		}
		$sSql .= ", $sField = '".addslashes($aDatiDB['A_'.$sField])."'";
	}
	$sSql .= " WHERE id_rec = ".reqVar('idr');
	//return $sSql;
	$objDB->exec($sSql);
	header("location: recList.php?id=".$oMod->idModulo."&xsn=".XSN);
	exit;
}
